package com.example.lab4.Controller;
import com.example.lab4.Entity.*;
import com.example.lab4.Mapper.*;
import com.example.lab4.common.Result;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import java.io.BufferedOutputStream;
import java.io.*;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;

@Controller
public class ProduceController {
    @Autowired
    private ProduceDao producedao;
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @RequestMapping("/Produce")
    public String Goto车次(Model m){
        //获取查询耗时
        Instant now = Instant.now();
        List<String> 管理员s = producedao.FindManager();
        long used = ChronoUnit.MILLIS.between(now, Instant.now());
        List<String> 车站s = producedao.FindStation();
        m.addAttribute("管理员",管理员s);
        m.addAttribute("车站",车站s);
        //将相关信息写入文件
        FileWriter fw = null;
        try {
            File f=new File("Search.txt");
            fw = new FileWriter(f, true);
        } catch (IOException e) {e.printStackTrace();}
        PrintWriter pw = new PrintWriter(fw);
        pw.println("Select data:   " + df.format(new Date()) + "    " + used + "ms");
        pw.flush();
        try {
            fw.flush(); pw.close(); fw.close();
        } catch (IOException e) {e.printStackTrace();}
        return "Produce";
    }

    @PostMapping("/producecome")
    @ResponseBody
    public Result come(){
        //获取插入耗时
        Instant now2 = Instant.now();
        producedao.addPerson();
        long used2 = ChronoUnit.MILLIS.between(now2, Instant.now());
        //将相关信息写入文件
        FileWriter fw2 = null;
        try {
            File f=new File("Insert.txt");
            fw2 = new FileWriter(f, true);
        } catch (IOException e) {e.printStackTrace();}
        PrintWriter pw2 = new PrintWriter(fw2);
        pw2.println("Insert data:   " + df.format(new Date()) + "    " + used2 + "ms");
        pw2.flush();
        try {
            fw2.flush(); pw2.close(); fw2.close();
        } catch (IOException e) {e.printStackTrace();}
        return Result.ok();
    }

    @PostMapping("/produceadd")
    @ResponseBody
    public Result add(@RequestBody String 站名){
        producedao.insertStation(站名);
        return Result.ok();
    }

    @PostMapping("/producedele")
    @ResponseBody
    public Result dele(@RequestBody String 站名){
        producedao.deleteStation(站名);
        return Result.ok();
    }

    @PostMapping("/produceedit")
    @ResponseBody
    public Result dele(String phone, String opass, String npass){
        producedao.editStation(phone, opass, npass);
        return Result.ok();
    }
}
